/**
 * 评论服务
 * @module services/commentService
 */

import api from './api';

/**
 * 获取文章评论
 * @param {string} articleId - 文章ID
 * @returns {Promise<Object>} 评论列表
 */
export const getArticleComments = async (articleId) => {
  try {
    return await api.get(`/comments/article/${articleId}`);
  } catch (error) {
    console.error('获取文章评论失败:', error);
    throw error;
  }
};

/**
 * 创建评论
 * @param {string} articleId - 文章ID
 * @param {Object} comment - 评论数据
 * @returns {Promise<Object>} 创建结果
 */
export const createComment = async (articleId, comment) => {
  try {
    return await api.post(`/comments/article/${articleId}`, comment);
  } catch (error) {
    console.error('创建评论失败:', error);
    throw error;
  }
};

/**
 * 获取所有评论(管理员)
 * @param {Object} params - 查询参数
 * @returns {Promise<Object>} 评论列表
 */
export const getAllComments = async (params = {}) => {
  try {
    return await api.get('/comments', { params });
  } catch (error) {
    console.error('获取所有评论失败:', error);
    throw error;
  }
};

/**
 * 审核评论
 * @param {string} id - 评论ID
 * @param {boolean} approved - 是否通过
 * @returns {Promise<Object>} 审核结果
 */
export const approveComment = async (id, approved) => {
  try {
    return await api.put(`/comments/${id}/approve`, { approved });
  } catch (error) {
    console.error('审核评论失败:', error);
    throw error;
  }
};

/**
 * 删除评论
 * @param {string} id - 评论ID
 * @returns {Promise<Object>} 删除结果
 */
export const deleteComment = async (id) => {
  try {
    return await api.delete(`/comments/${id}`);
  } catch (error) {
    console.error('删除评论失败:', error);
    throw error;
  }
}; 